//
//  PhoneNumberViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/1/20.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "PhoneNumberViewController.h"

@interface PhoneNumberViewController ()
{
    NSMutableArray * nArr;
    
    NSMutableString * phoneNumStr;
}
@end

@implementation PhoneNumberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
   // [self addBackItem];
    self.navigationItem.title = @"手机号";
    //[self addNavgationBar];
    [self addrightItem];
    // Do any additional setup after loading the view.
}
-(void)main
{
    phoneNumStr = [[NSMutableString alloc]init];
    nArr = [[NSMutableArray alloc]init];
    for (int i = 0; i<10; i++) {
        [nArr addObject:[NSString stringWithFormat:@"%i",i]];
    }
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(SIZETO6W(29), 64+SIZETO6H(53), SCREEN_WIDTH-SIZETO6W(29)*2, 47)];
    view.backgroundColor = [UIColor clearColor];
    view.layer.masksToBounds = YES;
    view.layer.borderColor = [UIColorFromRGB(0xdddddd) CGColor];
    view.layer.borderWidth = 2.0f;
    view.layer.cornerRadius = 20;
    [self.view addSubview:view];
    
    
    UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(SIZETO6W(20), 0, view.width-SIZETO6W(20)-SIZETO6W(15)-15, 49)];
    UIColor * color = UIColorFromRGB(0x333944);
    textField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"请输入手机号码" attributes:@{NSForegroundColorAttributeName:color}];
    [textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    textField.tag = 100;
    textField.keyboardType = UIKeyboardTypeNumberPad;
    textField.textColor = color;
    [view addSubview:textField];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 30, 30);
    [btn setImage:[UIImage imageNamed:@"关闭按钮2-"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clearBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    textField.rightView = btn;
    textField.rightViewMode = UITextFieldViewModeWhileEditing;
    
}
-(void)textChange:(UITextField *)textField
{
    if (textField.text.length > phoneNumStr.length) {
        if (textField.text.length <= 11 ) {
            
            NSString * s = [textField.text substringWithRange:NSMakeRange(textField.text.length-1, 1)];
            if ([nArr containsObject:s] ) {
                [phoneNumStr appendString:s];
            }
        }
    }
    else if([textField.text isEqualToString:@""])
    {
        phoneNumStr = [[NSMutableString alloc]init];
    }
    else
    {
        NSString * str = [phoneNumStr substringToIndex:(phoneNumStr.length-1)];
        phoneNumStr = [[NSMutableString alloc]initWithString:str];
    }
    
    textField.text = phoneNumStr;

}
-(void)clearBtnClick:(id)sender
{
    UITextField * textField = (UITextField *)[self.view viewWithTag:100];
    textField.text = @"";
    phoneNumStr = [[NSMutableString alloc]init];

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
#pragma mark - addItems
-(void)addrightItem
{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 40, 40);
    
    [btn setTitle:@"确认" forState:UIControlStateNormal];
    [btn setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(sureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightItem;
}
-(void)sureBtnAction:(UIButton *)btn
{
    UITextField * textField = (UITextField *)[self.view viewWithTag:100];
    
    if ([self isEmpty:textField.text]) {
        textField.text = @"";
        return ;
    }
    
    if (![textField.text isEqualToString:@""]) {
        [self.delegate changePhoneNumberWithnumber:textField.text];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(BOOL) isEmpty:(NSString *) str {
    
    if (!str) {
        return true;
    } else {
        //A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        
        //Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }
}


#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
